package com.core.baseDao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.core.baseDao.BaseDao;
import com.core.baseDao.extend.Pager;

/**
 * 
 * DAO基础接口，实现对POJO的基本CRUD的操�? * @author <a
 * href="mailto:chshxuan@163.com">csx</a>
 * 
 * @param <T>
 *            POJO类型
 * @param <PK>
 *            主键类型，如可以为Long,Integer,String�?
 */
@SuppressWarnings("unchecked")
public class HibernateBaseDao<T, PK extends Serializable> extends
		HibernateDaoSupport implements BaseDao<T, PK> {

	public void saveOrUpdate(T t) {
		this.getHibernateTemplate().saveOrUpdate(t);
	}

	public T load(Class<T> entityClass, PK id) {
		T load = (T) getHibernateTemplate().load(entityClass, id);
		return load;
	}

	public T get(Class<T> entityClass, PK id) {
		T load = (T) getHibernateTemplate().get(entityClass, id);
		return load;
	}

	public boolean contains(T t) {
		return getHibernateTemplate().contains(t);
	}

	public void delete(T t, LockMode lockMode) {
		getHibernateTemplate().delete(t, lockMode);
	}

	public void delete(T t) {
		getHibernateTemplate().delete(t);
	}

	public void deleteAll(Collection<T> entities) {
		getHibernateTemplate().deleteAll(entities);
	}

	public List<T> find(String queryString, Object value) {
		List<T> find = (List<T>) getHibernateTemplate()
				.find(queryString, value);
		return find;
	}

	public List<T> find(String queryString, Object[] values) {
		List<T> find = (List<T>) getHibernateTemplate().find(queryString,
				values);
		return find;
	}

	public List<T> find(String queryString) {
		return (List<T>) getHibernateTemplate().find(queryString);
	}

	public void refresh(T t) {
		getHibernateTemplate().refresh(t);
	}

	public Serializable save(T t) {
		return getHibernateTemplate().save(t);
	}

	public void saveOrUpdateAll(Collection<T> entities) {
		getHibernateTemplate().saveOrUpdateAll(entities);
	}

	public void update(T t, LockMode lockMode) {
		getHibernateTemplate().update(t, lockMode);
	}

	public void update(T t) {
		getHibernateTemplate().update(t);
	}

	public List<T> getAll(Class<T> entityClass) {
		return getHibernateTemplate().loadAll(entityClass);

	}

	public List<T> findByNamedQuery(String queryName) {
		return getHibernateTemplate().findByNamedQuery(queryName);
	}

	public List<T> findByNamedQuery(String queryName, Object value) {
		return getHibernateTemplate().findByNamedQuery(queryName, value);
	}

	public List<T> findByNamedQuery(String queryName, Object[] values) {
		return getHibernateTemplate().findByNamedQuery(queryName, values);
	}

	@Override
	public Object findUnique(final String queryString, final Object[] values) {
		return (Object) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString);
				if (values != null) {
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}
				return query.uniqueResult();
			}
		});
	}

	@Override
	public boolean exists(Class<T> entityClass, PK id) {
		T entity = (T) super.getHibernateTemplate().get(entityClass, id);
		return entity != null;
	}

	@Override
	public HibernateTemplate getHibernate() {
		return getHibernateTemplate();
	}

	/**
	 * 
	 * 使用hql 语句进行操作
	 * @param hql
	 *            需要执行的hql语句
	 * @param offset
	 *            设置开始位置
	 * @param length
	 *            设置读取数据的记录条数
	 * @return List 返回所需要的集合。
	 */
	public Pager getByPage(final String hql,final Pager pager) {
		List list = getHibernateTemplate().executeFind(
		new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult((pager.getCurrPage()-1)*pager.getPageSize());
				query.setMaxResults(pager.getPageSize());
				List list = query.list();
				pager.setContent(list);
				pager.setAllRows(findTotalCount(hql));
				return list;
			}
		});
		return pager;
	}
	
	/**
	 * 获取记录总数
	 * @param hql
	 * @return
	 */
	public int findTotalCount(final String hql) {   
		  List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				return query.list();
			}
		});
		  return list.size();
	}   

	

}